//
//  newsFlashCollectionViewCell.m
//  Cars' home
//
//  Created by dapeng on 15/11/30.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "newsFlashCollectionViewCell.h"
#import "newsFlashTableViewCell.h"
#import "flashTwoModel.h"
@implementation newsFlashCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self tableViews];
        [self addSubview:self.tableView];
        [_tableView release];
        
        self.backgroundColor = [UIColor clearColor];
        [self header];
        [self footer];
        [self.tableView headerBeginRefreshing];
        [self changeColor];
    }
    return self;
}
- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
    
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView registerClass:[newsFlashTableViewCell class] forCellReuseIdentifier:@"newsFlashTableViewCellIndentifier"];
    
}
- (void)header {
    _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [_tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SAPNetWorkTool getWithUrl:newsFlash parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                self.oneModel2 = [[flashOneModel alloc] initWithDataSource:dic];
                _HUD.hidden = YES;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            [_tableView reloadData];
            [_tableView headerEndRefreshing];
        });
    }];
}
- (void)footer {
    [_tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            NSString *string = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/news/fastnewslist-pm1-b0-l0-s20-lastid%@.json", self.twoModel2.lastid];
            [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                flashOneModel *oneModel = [[flashOneModel alloc] initWithDataSource:dic];
                [self.oneModel2.listArray addObjectsFromArray:oneModel.listArray];
                [_tableView reloadData];
                [_tableView footerEndRefreshing];
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
        });
        
    }];
    
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 69);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.oneModel2.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    newsFlashTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsFlashTableViewCellIndentifier"];
    flashTwoModel *twoModel = self.oneModel2.listArray[indexPath.row];
    self.twoModel2 = self.oneModel2.listArray[indexPath.row];
    cell.twoModel2 = twoModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    flashTwoModel *twoModel = self.oneModel2.listArray[indexPath.row];
    
    NSString *str = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov5.0.5/content/news/fastnewsshared-pm1-n%@.html", twoModel.id];
   
   
    [self.delegate passFlashValue:str];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_SIZE.height / 2 - 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
@end
